<?php
/**
 * 建行返回地址
 *
 * lijianjun 161201
 */

//返回处理
$ThirdSysID = $_POST['ThirdSysID'];
$TxCode = $_POST['TxCode'];
$RequestType = $_POST['RequestType'];
$Data = $_POST["Data"] ;
$Auth = $_POST["Auth"] ;
$MD5Key = "TestMD5Key1";

$myauth = md5_encrypt($ThirdSysID.$TxCode.$RequestType.$Data.$MD5Key);
//echo $ThirdSysID."   ".$TxCode."  ".$RequestType."  ".$Data."  ".$Auth."    ".$myauth;exit;
if($Auth==$myauth)
{
    $DesKey = "TestDesKey1";
    $decryptData = des_decrypt($Data,$DesKey);
}else{
    echo "md5校验不成功，数据有可能被更改";exit;
}
//显示返回的数据到表单中

//用到的一些函数
/**
 **DES 加密
 **$data 待加密明文
 **$deskey 加密秘钥
 **/
function des_encrypt($data,$deskey){
    $blocksize = mcrypt_get_block_size(MCRYPT_DES,MCRYPT_MODE_ECB);
    $pad = $blocksize - (strlen($data) % $blocksize);
    $data1 = $data. str_repeat(chr($pad),$pad);

    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB),MCRYPT_RAND); //初始化向量
    @$data_encrypt  = mcrypt_encrypt(MCRYPT_DES,$deskey,$data1,MCRYPT_MODE_ECB);//加密函数
    $datastr = bin2hex($data_encrypt);
    return $datastr;
}

/**
 **DES 解密
 **$data 待解密密文
 **$deskey 加密秘钥
 **/
function  des_decrypt($endata,$deskey){
    $de_datastr = $endata !== false && preg_match('/^[0-9a-fA-F]+$/i',$endata) ? pack('H*',$endata):false;
    @$data_decrypt = mcrypt_decrypt(MCRYPT_DES,$deskey,$de_datastr,MCRYPT_MODE_ECB,null);//解密函数
    $ret = _pkcs5Unpad($data_decrypt);
    $de_data = trim($ret);
    return $de_data;
}

/**
 **MD5 加密
 **/
function md5_encrypt($data){
    return md5($data);
}

function _pkcs5Unpad($text){
    $pad = ord($text{strlen($text)-1});
    if($pad > strlen($text)) return false;
    if(strspn($text,chr($pad),strlen($text)-$pad) != $pad) return false;
    $ret = substr($text,0,-1*$pad);
    return $ret;
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>

<form action="http://qinyongshun.ccb.com:8000/alliance/thirdPartAPI.php" method="POST" >
    <strong>Data:</strong>
    <div>
        <?php echo $Data; ?><br/>
    </div>
    <strong>decryptData:</strong>
    <div>
        <?php echo $decryptData;?>
    </div>

</form>

</body>

</html>
